//================================================================================
//note:extract replace string in table
//in :all string table
//out:oldstring,newstring
//
//fild oldstring and replace to newstring
//function:	0x01:\CLR_A:clear after
//		0x02:\CLR_B:clear before
//		0x04:\CLR_L:clear line
//================================================================================


public class Extract{
	private int DBG=0;
	private int index;
	private int replacecount;

	private String[] oldstring;
	private String[] newstring;
	private byte[] function;

	public Extract(){
		oldstring=new String[1];
		newstring=new String[1];
		function=new byte[1];
		DBG=0;
		index=0;
	}


	public Extract(String orgString){
//DBG=1;
/*
System.out.println("0---------------------------");
System.out.println("oldstringlen:"+orgString.length());
for(int n=0;n<orgString.length();n++){
System.out.print(orgString.charAt(n));
}
System.out.println();
System.out.println("char:");
for(int n=0;n<orgString.length();n++){
System.out.print((byte)orgString.charAt(n));
}
System.out.println();
*/

		replacecount=0;

		String[] tmpString=orgString.split("\r\n"),tmpString_1;


		if(tmpString.length>0){
			String[] oldstring_1=new String[tmpString.length];
			String[] newstring_1=new String[tmpString.length];
/*
System.out.println("1---------------------------");
System.out.println("oldstring_1len:"+oldstring_1.length);
System.out.println("newstring_1len:"+newstring_1.length);
System.out.println("char:");
for(int p=0;p<tmpString.length;p++){
for(int n=0;n<tmpString[p].length();n++){
System.out.print((int)tmpString[p].charAt(n));
}
System.out.println();
}
System.out.println();
*/

			for(int i=0;i<tmpString.length;i++){
				if(DBG==1){
//					System.out.print(i+":");
//					System.out.println(tmpString[i]);
				}
				
				//remove first "\t"
				if(tmpString[i].indexOf("\t")==0){
					do{
						tmpString[i]=tmpString[i].replaceFirst("\t","");
					}
					while(tmpString[i].indexOf("\t")==0);
				}

				tmpString_1=tmpString[i].split("\t");


				if(tmpString_1.length>0){
/*
System.out.println("2---------------------------");
System.out.println("tmpString_"+i+":"+"len:"+tmpString_1.length+":"+tmpString_1[0]);
System.out.println("char:");
for(int n=0;n<tmpString_1[0].length();n++){
System.out.print((int)tmpString_1[0].charAt(n));
System.out.println(":"+tmpString_1[0].charAt(n));
}
*/
					if(tmpString_1[0].indexOf("////")==0){
						tmpString_1[0]="";
					}


					if(tmpString_1[0]!=""){

						oldstring_1[replacecount]=tmpString_1[0];
/*
System.out.println("3---------------------------");
System.out.println("old:"+oldstring_1[replacecount]);
System.out.println(tmpString_1[0]);

for(int n=0;n<tmpString_1[0].length();n++){
System.out.print((int)tmpString_1[0].charAt(n));
System.out.println(":"+tmpString_1[0].charAt(n));
}
*/

						if(   tmpString_1[0].indexOf("\n")==0 
						   || tmpString_1[0]=="\r\n"	//no data in this line
						   || tmpString_1[0]==null
						){
//System.out.println("4---------------------------");
							oldstring_1[replacecount]="";
							newstring_1[replacecount]="";
						}
						else if(tmpString_1[0].indexOf("//")==0){
							newstring_1[replacecount]="";
						}
						else if(tmpString_1.length>1){

//System.out.println("5---------------------------");
//System.out.print("len:"+tmpString_1[0].length());
//System.out.print(tmpString_1[0].indexOf("//"));
//System.out.println("+"+tmpString_1[0].indexOf("//"));
							newstring_1[replacecount]="";
							for(int j=0;j<tmpString_1.length;j++){
								if(tmpString_1[j]!=null && tmpString_1[j]!="\t"){
									newstring_1[replacecount]=tmpString_1[j];
								}
							}

						}
						else{
							newstring_1[replacecount]="";
						}


						if(oldstring_1[replacecount]!=""){
/*
System.out.println("6---------------------------");
System.out.println("replacecount:"+replacecount);
//System.out.println("char:"+(int)oldstring_1[replacecount].charAt(0));
System.out.println("old:"+oldstring_1[replacecount]);
System.out.println("new:"+newstring_1[replacecount]);
*/
							replacecount++;
						}

					}

				}
			}

			oldstring=new String[replacecount];
			newstring=new String[replacecount];
			function=new byte[replacecount];
//	\n	New line
//	\t	Tab
//	\b	Backspace
//	\r	Carriage return
//	\f	Formfeed
//	\\	Backslash
//	\'	Single quotation mark
//	\"	Double quotation mark
//	\d	Octal
//	\xd	Hexadecimal
//	\\ud	Unicode character

			for(int i=0;i<replacecount;i++){
				oldstring[i]=oldstring_1[i];
				newstring[i]=newstring_1[i];

				oldstring[i]=oldstring[i].replace("\\n","\r\n");
				newstring[i]=newstring[i].replace("\\n","\r\n");

				oldstring[i]=oldstring[i].replace("\\t","\t");
				newstring[i]=newstring[i].replace("\\t","\t");

				oldstring[i]=oldstring[i].replace("\\b","\b");
				newstring[i]=newstring[i].replace("\\b","\b");

				oldstring[i]=oldstring[i].replace("\\r","\r");
				newstring[i]=newstring[i].replace("\\r","\r");

				oldstring[i]=oldstring[i].replace("\\f","\f");
				newstring[i]=newstring[i].replace("\\f","\f");

				oldstring[i]=oldstring[i].replace("\\\\","\\");
				newstring[i]=newstring[i].replace("\\\\","\\");

				if(oldstring[i].indexOf("\\CLR_A")==0){
					oldstring[i]=oldstring[i].replace("\\CLR_A","");
					newstring[i]="";
					function[i]|=0x01;
				}
				if(oldstring[i].indexOf("\\CLR_B")==0){
					oldstring[i]=oldstring[i].replace("\\CLR_B","");
					newstring[i]="";
					function[i]|=0x02;
				}
				if(oldstring[i].indexOf("\\CLR_L")==0){
					oldstring[i]=oldstring[i].replace("\\CLR_L","");
					newstring[i]="";
					function[i]|=0x04;
				}
			}
/*
System.out.println("7---------------------------");
System.out.println("replacecount:"+replacecount);

for(int i=0;i<oldstring.length;i++){
	System.out.println("old:"+oldstring[i]+"|");
		System.out.print("c:");
		for(int j=0;j<oldstring[i].length();j++){
			System.out.print((byte)oldstring[i].charAt(j));
		}
	System.out.println();
	System.out.println("new:"+newstring[i]+"|");

//	System.out.println("old:"+oldstring_1[i]);
//	System.out.println("new:"+newstring_1[i]);
}
*/
		}
	}



	//DBG==2
	public String getPartOfChar(String orgString,String sepString,int i){

   		String tString=new String("");

		String[] tmpString;

		tmpString=orgString.split(sepString);
		
		if(DBG==2){
			System.out.println("Extract.getPartOfChar()-------------------");
			System.out.println("input:"+orgString+" "+sepString+" "+i);
			System.out.println();
			System.out.println("size:"+tmpString.length);
			if(tmpString.length>0){
				for(int j=0;j<tmpString.length;j++){
					System.out.print(j+":");
					System.out.println(tmpString[j]);
				}
			}
		}

		if(tmpString.length>0){
			if(tmpString[0]==""){
				i++;
			}
		}

		if(tmpString.length==0 || tmpString.length==1 || i>=tmpString.length){//all the same or no find
			tString="";
		}
		else
			tString=tmpString[i];

		if(DBG==2){
			System.out.println();
			System.out.println("return:"+i+":"+tString);
			System.out.println("==========================================");
		}

		return tString;
	}

	

	public void setDBG(int s){
					System.out.println("DBG:"+DBG);
		DBG=s;
					System.out.println("DBG:"+DBG);					
					System.out.print(DBG);
	}
	
	public String[] getOldString(){
		return oldstring;
	}

	public String[] getNewString(){
		return newstring;
	}
	public byte[] getFunction(){
		return function;
	}

	public int getReplaceCount(){
		return replacecount;
	}
}


